package com.mankind.ssm.service;

import com.mankind.ssm.common.ROLE;
import com.mankind.ssm.mapper.RoleMapper;
import com.mankind.ssm.mapper.UserRoleMapper;
import com.mankind.ssm.pojo.Role;
import com.mankind.ssm.pojo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mankind on 2017/7/14.
 */
@Service("UserRoleService")
public class UserRoleServiceImpl implements UserRoleService  {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    public boolean managerCheck(Integer userId){
        List<UserRole> list=userRoleMapper.selectByUserId(userId);
        int size=list.size();
        for(int i=0;i<size;i++){
            if(list.get(i).getRoleId()== ROLE.MANAGER){
                return true;
            }
        }
        return false;
    }
    public List<Role> getRoleById(Integer userId){
        List<UserRole> list=userRoleMapper.selectByUserId(userId);
        List<Role> roleList=new ArrayList<Role>();
        int size=list.size();
        System.out.println("size:"+size);
        for(int i=0;i<size;i++){
            System.out.println(list.get(i).getRoleId());
            roleList.add(roleMapper.selectByPrimaryKey(list.get(i).getRoleId()));

        }
        return roleList;
    }

    public void assignRole(Integer userId, String[] roleIdList) {
            userRoleMapper.deleteByUserId(userId);
            for(int i=0;i<roleIdList.length;i++){
                Integer roleId=Integer.parseInt(roleIdList[i]);
                userRoleMapper.insert(new UserRole(userId,roleId));
            }
    }
}
